<!-- 印章授权审核 -->
<template>
	<view class="page">
		<u--form labelPosition="top" :model="model" ref="form" :rules="rules" labelWidth="auto" :labelStyle="{
				fontSize: '14px',
				color: '#B2B2B2',
				lineHight: '20px'
			}">
			<view class="basic-style">
				<view class="basic-title basic-border">新增</view>
				<u-form-item label="单据编号" prop="billNo" :borderBottom="false">
					<u--input disabled placeholder="请输入单据编号" v-model="model.billNo"></u--input>
				</u-form-item>
				<u-form-item label="申请人" prop="proposer" :borderBottom="false">
					<h-select-person-input style="width: 100%" disabled v-model="model.proposer" placeholder="请选择申请人"
						border="surround" suffixIcon="arrow-down-fill" :suffixIconStyle="{color: '#B2B2B2'}"
						objectField="model.proposer"></h-select-person-input>
				</u-form-item>
				<u-form-item label="申请部门" prop="proposerDeptId" :borderBottom="false">
					<h-select-depart-input disabled v-model="model.proposerDeptId" :departName="model.proposerDeptText"
						placeholder="请选择申请部门" border="surround" style="width: 100%" suffixIcon="arrow-down-fill"
						:suffixIconStyle="{ color: '#B2B2B2' }"
						:title.sync="model.proposerDeptText"></h-select-depart-input>
				</u-form-item>
				<u-form-item label="申请时间" prop="createTime" :borderBottom="false">
					<u--input v-model="model.createTime" disabled placeholder="请选择申请时间" border="surround"
						suffixIcon="calendar-fill" :suffixIconStyle="{color: '#B2B2B2'}"></u--input>
				</u-form-item>
				<u-form-item label="所属团队" prop="teamId" required>
					<h-select-team-input style="width: 100%" v-model="model.teamId" objectField="model.teamId"
						placeholder="请选择所属团队" border="surround" :tdlx="3" suffixIcon="arrow-down-fill"
						:suffixIconStyle="{color: '#B2B2B2'}"></h-select-team-input>
				</u-form-item>
				<u-form-item label="损坏物品" prop="damageGoods" required>
					<u--input v-model="model.damageGoods" placeholder="请输入损坏物品"></u--input>
				</u-form-item>
				<u-form-item label="损坏区域" prop="damageZone" :borderBottom="false" ref="item1">
					<u--textarea v-model="model.damageZone" confirmType="done" placeholder="请输入损坏区域"
						:maxlength="500"></u--textarea>
				</u-form-item>
				<u-form-item label="备注" prop="remark" :borderBottom="false" ref="item1">
					<u--textarea v-model="model.remark" confirmType="done" placeholder="请输入备注"
						:maxlength="500"></u--textarea>
				</u-form-item>
			</view>
			<view class="basic-style">
				<view class="basic-title basic-border">附件信息</view>
				<view class="py-1 space-y-1" style="width: 100%">
					<view style="color: red">提示：请等待附件上传完成，再点击保存，否则附件无法保存成功！</view>
					<h-upload-file v-model="model.attachments" :isIcon="false" />
					<file-list v-if="model.attachments" v-model="model.attachments" :isDelete="true"></file-list>
				</view>
			</view>
			<view class="btn">
				<u-button type="primary" text="保存提交" :loading="loading" @click="saveAndCommit()"
					style="margin-right: 40rpx;" />
				<u-button type="primary" text="保存" :loading="loading" @click="save()" />
			</view>
		</u--form>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	import moment from 'moment'
	import {
		generatedFormNumber, // 获取单据编号
		addGoodsMaintenance,
	} from '@/api/index.js'
	export default {
		data() {
			return {
				id: '',
				model: {
					billNo: '', //单据编号
					proposer: '', // 申请人id
					proposerDeptId: '', //申请部门
					proposerDeptText: '', //申请部门
					createTime: '', // 申请时间
					teamId: '', //所属团队
					damageGoods: '', //损坏物品
					damageZone: '', //损坏区域
					remark: '', //备注
					attachments: null, //附件
				},
				rules: {
					teamId: [{
						required: true,
						message: '请选所属营团队!',
						trigger: 'change'
					}],
					damageGoods: [{
						required: true,
						message: '请输入损坏物品!',
						trigger: 'change'
					}],
				},
				loading: false,
			}
		},
		onLoad(options) {
			if (options.id) {
				// 编辑
				this.edit(options.id)
				this.id = options.id
			} else {
				// 添加
				this.add()
			}
		},
		methods: {
			// 添加
			add() {
				this.getGeneratedFormNumber()
				this.model.createTime = moment().format('YYYY-MM-DD HH:mm:ss'); // 申请时间
				this.model.proposer = uni.getStorageSync('userInfo').empId; //申请人id
				this.model.proposerDeptId = uni.getStorageSync('userInfo').deptId; //申请部门id
			},
			// 获取单据编号
			getGeneratedFormNumber() {
				generatedFormNumber({
					prefix: 'WPWX',
					subSystemCode: '04'
				}).then(res => {
					if (res.success) {
						this.$set(this.model, 'billNo', res.result)
					}
				})
			},
			// 保存提交
			saveAndCommit() {

			},

			// 保存
			async save() {
				const val = await this.$refs.form.validate()
				if (!val) return
				if (!this.id) {
					this.loading = true
					addGoodsMaintenance(this.model).then(res => {
						if (res.success) {
							uni.showToast({
								title: '保存成功',
								icon: 'none',
								success() {
									setTimeout(() => {
										uni.navigateBack({
											delta: 1
										});
									}, 500)
								}
							})
						}
					}).finally(() => this.loading = false)
				}
			}
		}
	}
</script>


<style lang="scss" scoped>
	@import url("@/assets/inputStyle.scss");
</style>